<template>
  <div class="questionnaire-container" :style="{ height: height }">
    <el-card class="questionnaire-card">
      <el-form :model="form" label-position="right">
        <el-row type="flex" justify="space-between" class="header-row">
          <el-col :span="12">
            <el-tag size="medium">题量：{{form.titleNum}}</el-tag>
          </el-col>
          <el-col :span="12" v-if="selectType === 1" class="text-right">
            <span class="score-label">总得分</span>
            <el-tag type="success">{{form.totalScore}}</el-tag>
          </el-col>
        </el-row>

        <div v-for="(item, index) in form.questionnaireTitles" :key="index" class="question-item">
          <el-divider></el-divider>
          <el-row class="question-header">
            <el-col :span="18">
              <span class="question-index">题目{{ index + 1 }}：</span>
              <span class="question-name">{{ item.topicName }}</span>
            </el-col>
            <el-col :span="6" class="text-right">
              <el-tag size="small">{{ item.questionType }}</el-tag>
            </el-col>
          </el-row>

          <div class="answer-section">
            <template v-if="item.questionType === '单选题'">
              <el-radio-group v-if="selectType === 0" v-model="saveValues[index]">
                <el-radio 
                  v-for="option in item.questionnaireAnswersList"
                  :key="option.solutionId"
                  :label="option.solutionId"
                >
                  {{ option.solutionName }}
                </el-radio>
              </el-radio-group>
              <el-radio-group v-else v-model="form.result[index][0]" disabled>
                <el-row v-for="option in item.questionnaireAnswersList" :key="option.solutionId" class="option-row">
                  <el-col :span="18">
                    <el-radio :label="option.solutionId">{{ option.solutionName }}</el-radio>
                  </el-col>
                  <el-col :span="6" class="text-right">
                    <span class="score-text">得分：<el-tag size="small" type="info">{{option.score}}</el-tag></span>
                  </el-col>
                </el-row>
              </el-radio-group>
            </template>

            <template v-else-if="item.questionType === '多选题'">
              <el-checkbox-group 
                v-model="selectedValues[index]" 
                @change="(val) => handleMultipleChange(val, index)"
              >
                <el-row v-for="option in item.questionnaireAnswersList" 
                  :key="option.solutionId" 
                  class="option-row"
                >
                  <el-col :span="18">
                    <el-checkbox 
                      :label="option.solutionId"
                      :disabled="selectType === 1"
                    >
                      {{ option.solutionName }}
                    </el-checkbox>
                  </el-col>
                  <el-col :span="6" v-if="selectType === 1" class="text-right">
                    <span class="score-text">得分：<el-tag size="small" type="info">{{option.score}}</el-tag></span>
                  </el-col>
                </el-row>
              </el-checkbox-group>
            </template>
          </div>
        </div>

        <div class="action-buttons" v-if="selectType === 0">
          <el-button type="primary" @click="tijiao">提交</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getTitleByAnswer, selectQuestionnaireTitleBySend } from "@/api/questionnaire/title";
import { saveValues } from "@/api/questionnaire/enroll";

export default {
  name: "answer",
  props: {
    questionnaireId: {
      type: String,
      default: null
    },
    selectType: {
      type: Number,
      default: 0
    },
    orgId: {
      type: Number,
      default: null
    },
    height: {
      type: String,
      default: '100%'
    }
  },
  data() {
    return {
      titleAnswer: null,
      form: {},
      selectedValues: [],
      saveValues: [],
    }
  },
  mounted() {
    this.selectTitle();
  },
  methods: {
    selectTitle() {
      const request = this.orgId !== null
        ? selectQuestionnaireTitleBySend(this.questionnaireId, this.orgId)
        : getTitleByAnswer(this.questionnaireId);

      request.then(res => {
        this.form = res.data;
        this.selectedValues = this.form.questionnaireTitles.map(() => []);
        this.saveValues = this.form.questionnaireTitles.map(() => []);
        
        if (this.selectType === 1) {
          this.selectedValues = this.form.result;
          this.saveValues = this.form.result;
        }
      });
    },
    handleMultipleChange(value, index) {
      this.saveValues[index] = value;
    },
    tijiao() {
      const emptyIndices = this.saveValues.findIndex(item => !item || item.length === 0);
      
      if (emptyIndices !== -1) {
        this.$message.error(`第${emptyIndices + 1}题未完成作答，请填写完整后提交`);
        return;
      }

      saveValues(this.saveValues, this.questionnaireId).then(() => {
        this.$message.success("答题成功");
        this.$emit("change");
      });
    },
    cancel() {
      this.$emit("change");
    }
  }
}
</script>

<style lang="scss" scoped>
.questionnaire-container {
  padding: 20px;
  box-sizing: border-box;
  overflow: hidden;
  
  .questionnaire-card {
    height: 100%;
    overflow-y: auto;
    
    :deep(.el-card__body) {
      height: 100%;
      overflow-y: auto;
    }
  }
  
  .header-row {
    margin-bottom: 20px;
  }

  .question-item {
    margin-bottom: 30px;
  }

  .question-header {
    margin-bottom: 15px;
  }

  .question-index {
    font-weight: bold;
    margin-right: 8px;
  }

  .question-name {
    font-size: 14px;
  }

  .answer-section {
    padding: 10px 0;
  }

  .option-row {
    margin-bottom: 10px;
  }

  .score-label {
    margin-right: 10px;
  }

  .score-text {
    color: #606266;
  }

  .action-buttons {
    margin-top: 30px;
    text-align: center;
  }

  .text-right {
    text-align: right;
  }
}
</style>
